package com.icreate.storm.methods.common;

import org.apache.storm.tuple.Values;
import sun.security.util.Length;

import java.util.Arrays;
import java.util.Iterator;
import java.util.StringTokenizer;

/**
 * Created by icreate on 2017/5/18.
 */
public class StringMethod {
    private static String PACK_SEPARATOR = "`";

    public static String getXdrTyp(String source) {
        int index = source.indexOf(PACK_SEPARATOR);
        return source.substring(0, index);
    }

    public String getXdrTyp2(String source) {
        String[] strSplit = source.split(PACK_SEPARATOR, 2);
        return strSplit[0];
    }

    public int countOfChart(String str, String key) {
        int index = 0;
        int count = 0;
        while (index >= 0) {
            index = str.indexOf(key, index + 1);
            count++;
        }
        return count - 1;
    }

    public String getSplitOneCell(CharToken token, int index) {
        StringMethod.ByteSplitor lineSplitor = new StringMethod.ByteSplitor(token.getValue(), token.getBegin(), token.getLength(), ",".toCharArray());
        int count = 0;
        while (lineSplitor.hasNext()) {
            CharToken token1 = lineSplitor.next();
            if (count == index) {
                return token1.getString();
            }
            count++;
        }
        return "";
    }

    public static class ByteSplitor implements Iterator<CharToken> {
        private char[] value;
        private int offset;
        private int index;
        private int length;
        private char[] splitor;
        private CharToken token = new CharToken();

        public ByteSplitor(char[] value, char[] splitor) {
            this(value, 0, value.length, splitor);
        }

        public ByteSplitor(char[] value, int offset, int length, char[] splitor) {
            this.value = value;
            this.offset = offset;
            this.length = length;
            this.splitor = splitor;
        }

        @Override
        public boolean hasNext() {
            return index < length - 1;
        }

        @Override
        public CharToken next() {
            if (!hasNext())
                return null;
            int begin = index;
            int end = indexOf(value, offset, length, splitor, 0, splitor.length, index);
            if (end >= 0) {
                index = end + splitor.length;
                token.reset(value, begin, end - begin);
                return token;
            } else {
                index = length - 1;
                token.reset(value, begin, length - begin);
                return token;
            }

        }

//        private int indexOf(char[] splitor, int fromIndex) {
//            return indexOf(value, offset, value.length,
//                    splitor, 0, splitor.length, fromIndex);
//        }

        static int indexOf(char[] source, int sourceOffset, int sourceCount,
                           char[] target, int targetOffset, int targetCount,
                           int fromIndex) {
            if (fromIndex >= sourceCount) {
                return (targetCount == 0 ? sourceCount : -1);
            }
            if (fromIndex < 0) {
                fromIndex = 0;
            }
            if (targetCount == 0) {
                return fromIndex;
            }

            char first = target[targetOffset];
            int max = sourceOffset + (sourceCount - targetCount);

            for (int i = sourceOffset + fromIndex; i <= max; i++) {
            /* Look for first character. */
                if (source[i] != first) {
                    while (++i <= max && source[i] != first) ;
                }

            /* Found first character, now look at the rest of v2 */
                if (i <= max) {
                    int j = i + 1;
                    int end = j + targetCount - 1;
                    for (int k = targetOffset + 1; j < end && source[j]
                            == target[k]; j++, k++)
                        ;

                    if (j == end) {
                    /* Found whole string. */
                        return i - sourceOffset;
                    }
                }
            }
            return -1;
        }
    }

    public static class CharToken {
        private int begin = 0;
        private int length = 0;
        private char[] value;

        public CharToken() {
        }

        public CharToken(char[] value, int begin, int length) {
            this.begin = begin;
            this.length = length;
            this.value = value;
        }

        public void reset(char[] value, int begin, int length) {
            this.begin = begin;
            this.length = length;
            this.value = value;
        }

        public int getBegin() {
            return begin;
        }

        public int getLength() {
            return length;
        }

        public char[] getValue() {
            return value;
        }

        public long getLong() {
            return Long.valueOf(getString());
        }

        public String getString() {
            return new String(value, begin, length);
        }
    }
}
